package site;


import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.*;
import javax.servlet.http.*;

public class HttpRequest extends HttpServlet {

    private ProcessRequest processRequest;
    private String [] arguments;

    

    @Override
    public void init(ServletConfig config)
            throws ServletException {
           super. init(config);
    }



    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

         //   ../rarc/download
        if (request.getServletPath().equals("/download"))
        {
            if (request.getPathInfo() != null)
            {


                arguments = new String[1];
                arguments[0] = request.getPathInfo().substring(1);

                Logger.getLogger(HttpServlet.class.getName()).log(Level.INFO, "("+request.getRequestURI()+")");

                processRequest = new DownloadRequest(getServletContext());
                processRequest.process(response, arguments);

                return;
            }
        }
        //   ../rarc/update
        else if (request.getServletPath().equals("/update"))
        {

           arguments = new String[1];
            
            if( (arguments[0] = request.getParameter("v")) !=null)
            {


                Logger.getLogger(HttpServlet.class.getName()).log(Level.INFO, "("+request.getRequestURI()+ arguments[0]+")");


                processRequest = new UpdateRequest(getServletContext());
                processRequest.process(response, arguments);

                return;
            }
        }
        //   ../rarc/show
         else if (request.getServletPath().equals("/show"))/* /rarc/show */
        {
               //   ../rarc/show/list
               if( request.getPathInfo().equals("/list/"))
                {

                    arguments = new String[2];

                    if( (arguments[0] =  request.getParameter("l")) !=null &&
                          (arguments[1] = request.getParameter("o")) !=null )
                    {
                         if(arguments[0].equals("pt") || arguments[0].equals("en")){

                            Logger.getLogger(HttpServlet.class.getName()).log(Level.INFO, "("+request.getRequestURI()+"?l="+arguments[0] +"&o="+arguments[1] +")");

                            processRequest = new ListRequest(getServletContext());
                            processRequest.process(response, arguments);

                            return;
                         }
                     }
               }
              //   ../rarc/show/top
              else  if( request.getPathInfo().equals("/top/"))
              {
                    arguments = new String[1];

                     if( (arguments[0] =  request.getParameter("l"))!=null)
                    {
                         if(arguments[0].equals("pt") || arguments[0].equals("en")){

                             Logger.getLogger(HttpServlet.class.getName()).log(Level.INFO,"("+request.getRequestURI()+"?l="+arguments[0] +")");

                            processRequest = new TopRequest(getServletContext());
                            processRequest.process(response, arguments);
                            
                            return;
                         }                       
                     }
                }
               //   ../rarc/show/map
               else  if( request.getPathInfo().equals("/map/"))
               {
                    arguments = new String[1];

                     if( (arguments[0] =  request.getParameter("l"))!=null)
                    {
                         if(arguments[0] .equals("pt") ||arguments[0] .equals("en")){

                            Logger.getLogger(HttpServlet.class.getName()).log(Level.INFO, "("+request.getRequestURI()+"?l="+arguments[0] +")");

                            processRequest = new MapRequest(getServletContext());
                            processRequest.process(response, arguments);
                            
                            return;
                         }                       
                     }
               } 
        }

       //logs
       Logger.getLogger(HttpServlet.class.getName()).log(Level.WARNING, "---NOT OK---");

       //not found error
       response.setStatus(HttpServletResponse.SC_NOT_FOUND);
    }

    
    // netbeans code
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }

    // </editor-fold>

    
}


